package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.CatalogTask;
import com.wzcl.app.dao.model.EssayCourseQuestionBank;
import com.wzcl.app.dao.model.WorkShow;
import com.wzcl.app.dao.repository.CatalogTaskRepository;
import com.wzcl.app.dao.repository.EssayCourseQuestionBankRepository;
import com.wzcl.app.dao.repository.TaskEvaluateScoreRepository;
import com.wzcl.app.dao.repository.WorkShowRepository;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.TaskResourceDto;
import com.wzcl.app.service.CatalogTaskService;
import com.wzcl.app.utils.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2020年12月23日 08:49:00
 */
@Service
public class CatalogTaskServiceImpl implements CatalogTaskService {
    @Resource
    private CatalogTaskRepository catalogTaskRepository;
    @Resource
    private EssayCourseQuestionBankRepository essayCourseQuestionBankRepository;
    @Resource
    private WorkShowRepository workShowRepository;
    @Resource
    private TaskEvaluateScoreRepository taskEvaluateScoreRepository;

    @Override
    public List<CatalogTask> retrieveTask(Integer catalogId) {
        List<CatalogTask> allTask = catalogTaskRepository.findAllCatalog(catalogId);
        for (int outer = 0; outer < allTask.size(); outer++) {
            List<EssayCourseQuestionBank> list = new ArrayList<>();
            String questionList = allTask.get(outer).getQuestionList();
            List<String> listFromString = CollectionUtil.getListFromString(questionList, ",");
            //将String类型的List转换为Integer类型
            List<Integer> integerList = CollectionUtil.getIntegerFromString(listFromString);
            for (int inner = 0; inner < integerList.size(); inner++) {
                EssayCourseQuestionBank questionBank = essayCourseQuestionBankRepository.findQuestionBankById(integerList.get(inner));
                list.add(questionBank);
            }
            allTask.get(outer).setQuestions(list);
        }
        List<CatalogTask> listBySort = CollectionUtil.getListBySort(allTask, "getWhichNumber", "asc");
        return listBySort;
    }

    @Override
    @Transactional
    public RVO addTaskResource(TaskResourceDto taskResourceDto) {
        List<EssayCourseQuestionBank> questionBanks = taskResourceDto.getQuestionBanks();
        List<Integer> questionList = new ArrayList<>();
        // 更新章节任务资源
        for (int index = 0; index < questionBanks.size(); index++) {
            // 通过题库id查询题库
            EssayCourseQuestionBank questionBank = essayCourseQuestionBankRepository.findQuestionBankById(questionBanks.get(index).getId());
            String audio = questionBanks.get(index).getAudio();
            String questionAnalysisImage = questionBanks.get(index).getQuestionAnalysisImage();
            String questionAnalysisAudio = questionBanks.get(index).getQuestionAnalysisAudio();
            String image = questionBanks.get(index).getImage();
            // 添加题目与音频绑定
            essayCourseQuestionBankRepository.updateResourceById(audio, questionAnalysisAudio, questionAnalysisImage, image, questionBank.getId());
            //在任务中添加题库
            questionList.add(questionBanks.get(index).getId());
        }
        String questions = CollectionUtil.getStringFromList(questionList, ",");
        // 更新视频资源与任务题库
        catalogTaskRepository.updateVideoAndQuestion(taskResourceDto.getVideo(), questions, taskResourceDto.getTaskId());
        return RVO.success("添加资源成功");
    }

    @Override
    public List<CatalogTask> create(List<CatalogTask> list) {
        List<CatalogTask> catalogTasks = catalogTaskRepository.saveAll(list);
        return catalogTasks;
    }

    @Override
    @Transient
    public CatalogTask retrieveTaskById(Integer id) {
        List<EssayCourseQuestionBank> list = new ArrayList<>();
        CatalogTask catalogTask = catalogTaskRepository.findByTaskId(id);
        if (catalogTask == null){
            return null;
        }
        List<String> listFromString = CollectionUtil.getListFromString(catalogTask.getQuestionList(), ",");
        //将String类型的List转换为Integer类型
        List<Integer> integerList = CollectionUtil.getIntegerFromString(listFromString);
        for (int index = 0; index < integerList.size(); index++) {
            list.add(essayCourseQuestionBankRepository.findQuestionBankById(integerList.get(index)));
        }
        catalogTask.setQuestions(list);
        return catalogTask;
    }

    @Override
    public WorkShow getWorkShowByTaskId(Integer taskId) {
        //找到拍照任务id找到章节id
        Integer catalogId = catalogTaskRepository.findByTaskId(taskId).getEssayCourseCatalog().getId();
        //根据章节id找到作品秀并返回
        return workShowRepository.findWorkShowByCatalogId(catalogId);
    }
}